﻿/*****************************************************
 * 本类库的核心系 JNTemplate
 * (C) JNTemplate 2011
 * 作者：翅膀的初衷 QQ:4585839
 * 网址：http://www.JiNianNet.com
 *****************************************************/
#if test
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using JinianNet.JNTemplate.Process.Element;

namespace JinianNet.JNTemplate.Process
{
    public class Complex
    {
        public List<object> WordAnalysis(string str, params char[] separator)
        {
            return WordAnalysis(str, false, separator);
        }

        public List<object> WordAnalysis(string str, bool reserves, params char[] separator) {

            List<object> ListContair = new List<object>();
            List<char> separators = new List<char>(separator);
            int StartChar = 0;
            bool IsString = false;
            for (int i = 0; i < str.Length; i++) {
                if (str[i] == '"')
                    IsString = !IsString;
                if (separators.Contains(str[i])) {
                    if (!IsString) {
                        string str1;
                        if (StartChar == i)
                            str1 = string.Empty;
                        else
                            str1 = str.Substring(StartChar, i - StartChar);
                        ListContair.Add(WordAnalysis(str1));
                        StartChar = i + 1;
                        if (reserves) {
                            ListContair.Add(str[i].ToString());
                        }
                    }
                }
            }
            ListContair.Add(WordAnalysis(str.Substring(StartChar, str.Length - StartChar).Trim()));
            return ListContair;
        }

        public object WordAnalysis(string str)
        {
            if (string.IsNullOrEmpty(str))
                return str;
            str = str.Trim();
            if (str[0] == '"' && str[str.Length - 1] == '"')
                return str.Remove(str.Length - 1, 1).Remove(0, 1);
            else if (ParserRegex.Number.Match(str).Success)
                return int.Parse(str);
            else if (ParserRegex.Double.Match(str).Success)
                return double.Parse(str);
            else
                return new TemplateParser().AnalyserForString(str);// throw new Exception("语法错误");
        }


        #region
        public bool CompareObject(object x, object y, string operators) {
            if (x == null)
                return y == null;
            if (y == null)
                return x == null;
            string str1 = x.ToString(), str2 = y.ToString();
            if (ParserRegex.Number.Match(str1).Success && ParserRegex.Number.Match(str2).Success)
                return CompareExpression(int.Parse(str1), int.Parse(str2), operators);
            if (ParserRegex.Double.Match(str1).Success && ParserRegex.Double.Match(str2).Success)
                return CompareExpression(float.Parse(str1), float.Parse(str2), operators);
            return CompareExpression(str1, str2, operators);
        }

        public bool CompareExpression(float x, float y, string operators) {
            switch (operators) {
                case ">":
                    return x > y;
                case "<":
                    return x < y;
                case ">=":
                    return x >= y;
                case "<=":
                    return x <= y;
                case "==":
                    return x == y;
                case "!=":
                    return x != y;
                default:
                    return false;
            }
        }

        public bool CompareExpression(int x, int y, string operators) {
            switch (operators) {
                case ">":
                    return x > y;
                case "<":
                    return x < y;
                case ">=":
                    return x >= y;
                case "<=":
                    return x <= y;
                case "==":
                    return x == y;
                case "!=":
                    return x != y;
                default:
                    return false;
            }
        }

        public bool CompareExpression(string x, string y, string operators) {
            if (x == null)
                return y == null;
            if (y == null)
                return x == null;
            switch (operators) {
                case "==":
                    return x == y || x.Equals(y);
                case "!=":
                    return x != y || !x.Equals(y);
                default:
                    return false;
            }
        }

        #endregion

        #region
        public string Calculate(string x, string y, string op) {
            if (ParserRegex.Number.Match(x).Success && ParserRegex.Number.Match(y).Success)
                return Convert.ToString(CompareExpression(int.Parse(x), int.Parse(y), op));
            if (ParserRegex.Double.Match(x).Success && ParserRegex.Double.Match(y).Success)
                return Convert.ToString(CompareExpression(float.Parse(x), float.Parse(y), op));
            if (op == "+")
                return string.Concat(x, y);
            else
                return "类型错误";
        }
        public float Calculate(float x, float y, string op) {
            switch (op) {
                case "*":
                    return x*y;
                case "/":
                    return x / y;
                case "+":
                    return x + y;
                case "-":
                    return x - y;
                case "%":
                    return x % y;
                default:
                    throw new Exception("operator error");
            }
        }
        public float Calculate(int x, int y, string op) {
            switch (op) {
                case "*":
                    return x * y;
                case "/":
                    return x / y;
                case "+":
                    return x + y;
                case "-":
                    return x - y;
                case "%":
                    return x % y;
                default:
                    throw new Exception("operator error");
            }
        }
        #endregion

    }
}
#endif